/*
 * Copyright 2008 The Apache Software Foundation Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package org.apache.ibatis.ibator.generator;

import java.util.List;
import java.util.Properties;

import org.apache.ibatis.ibator.api.IntrospectedColumn;
import org.apache.ibatis.ibator.api.dom.java.CompilationUnit;
import org.apache.ibatis.ibator.api.dom.java.Field;
import org.apache.ibatis.ibator.api.dom.java.FullyQualifiedJavaType;
import org.apache.ibatis.ibator.api.dom.java.JavaVisibility;
import org.apache.ibatis.ibator.api.dom.java.Method;
import org.apache.ibatis.ibator.api.dom.java.Parameter;
import org.apache.ibatis.ibator.config.PropertyRegistry;
import org.apache.ibatis.ibator.internal.util.JavaBeansUtil;
import org.apache.ibatis.ibator.internal.util.StringUtility;

/**
 * @author Jeff Butler
 */
public abstract class AbstractJavaGenerator extends AbstractGenerator
{
	public abstract List<CompilationUnit> getCompilationUnits();

	public static Method getGetter(Field field)
	{
		Method method = new Method();
		method.setName(JavaBeansUtil.getGetterMethodName(field.getName(),
				field.getType()));
		method.setReturnType(field.getType());
		method.setVisibility(JavaVisibility.PUBLIC);
		StringBuilder sb = new StringBuilder();
		sb.append("return "); //$NON-NLS-1$
		sb.append(field.getName());
		sb.append(';');
		method.addBodyLine(sb.toString());
		return method;
	}

	public Method getJavaBeansGetter(IntrospectedColumn introspectedColumn)
	{
		FullyQualifiedJavaType fqjt = introspectedColumn
				.getFullyQualifiedJavaType();
		String property = introspectedColumn.getJavaProperty();

		Method method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setReturnType(fqjt);
		method.setName(JavaBeansUtil.getGetterMethodName(property, fqjt));
		ibatorContext.getCommentGenerator().addGetterComment(method,
				introspectedTable, introspectedColumn);

		StringBuilder sb = new StringBuilder();
		sb.append("return "); //$NON-NLS-1$
		sb.append(property);
		sb.append(';');
		method.addBodyLine(sb.toString());

		return method;
	}

	public Field getJavaBeansField(IntrospectedColumn introspectedColumn)
	{
		FullyQualifiedJavaType fqjt = introspectedColumn
				.getFullyQualifiedJavaType();
		String property = introspectedColumn.getJavaProperty();

		Field field = new Field();
		field.setVisibility(JavaVisibility.PRIVATE);
		field.setType(fqjt);
		field.setName(property);
		ibatorContext.getCommentGenerator().addFieldComment(field,
				introspectedTable, introspectedColumn);

		return field;
	}

	public Method getJavaBeansSetter(IntrospectedColumn introspectedColumn)
	{
		FullyQualifiedJavaType fqjt = introspectedColumn
				.getFullyQualifiedJavaType();
		String property = introspectedColumn.getJavaProperty();

		Method method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName(JavaBeansUtil.getSetterMethodName(property));
		method.addParameter(new Parameter(fqjt, property));
		ibatorContext.getCommentGenerator().addSetterComment(method,
				introspectedTable, introspectedColumn);

		StringBuilder sb = new StringBuilder();
		if (isTrimStringsEnabled() && introspectedColumn.isStringColumn())
		{
			sb.append("this."); //$NON-NLS-1$
			sb.append(property);
			sb.append(" = "); //$NON-NLS-1$
			sb.append(property);
			sb.append(" == null ? null : "); //$NON-NLS-1$
			sb.append(property);
			sb.append(".trim();"); //$NON-NLS-1$
			method.addBodyLine(sb.toString());
		}
		else
		{
			sb.append("this."); //$NON-NLS-1$
			sb.append(property);
			sb.append(" = "); //$NON-NLS-1$
			sb.append(property);
			sb.append(';');
			method.addBodyLine(sb.toString());
		}

		return method;
	}

	public boolean isTrimStringsEnabled()
	{
		Properties properties = ibatorContext
				.getJavaModelGeneratorConfiguration().getProperties();
		boolean rc = StringUtility.isTrue(properties
				.getProperty(PropertyRegistry.MODEL_GENERATOR_TRIM_STRINGS));
		return rc;
	}

	public String getRootClass()
	{
		String rootClass = introspectedTable
				.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_CLASS);
		if (rootClass == null)
		{
			Properties properties = ibatorContext
					.getJavaModelGeneratorConfiguration().getProperties();
			rootClass = properties.getProperty(PropertyRegistry.ANY_ROOT_CLASS);
		}

		return rootClass;
	}
}
